using System;
using System.Text;
using Sinacor.OMM.MessageEntity;
using Sinacor.OMM.Translator.Interfaces;
using System.Globalization;

namespace Sinacor.OMM.Translator.Translators.STM
{
    public class Message0103 : BaseTranslator, ITranslator
    {
        private static Message0103 _instance = null;

        private Message0103()
        {
        }

        public static Message0103 GetInstance()
        {
            if (_instance == null)
                _instance = new Message0103();

            return _instance;
        }

        #region ITranslator Members

        public Sinacor.OMM.MessageEntity.BaseMessage TranslateToRouterMessage()
        {
            return TranslateToMessage();
        }

        public Sinacor.OMM.MessageEntity.BaseMessage TranslateToProcessMessage()
        {
            return TranslateToMessageProcess();
        }

        #endregion

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        private Sinacor.OMM.MessageEntity.BaseMessage TranslateToMessage()
        {
            Sinacor.OMM.MessageEntity.STM.Message0103 _message = new Sinacor.OMM.MessageEntity.STM.Message0103();
            Sinacor.OMM.MessageEntity.BaseMessage baseMessage;
            int clientId;
            byte clientDigit;
            int port = 0;

            _message.MegaBolsaIdentificator = Sinacor.OMM.MessageEntity.Enum.MessagesMegaBolsaSource.CriacaoNegocio;
            _message.Source = Sinacor.OMM.MessageEntity.Enum.MessageSource.MegaBolsa;
            _message.Stock = OriginalMessage.Body.Substring(LayoutCatalog.STM.Message0103.Stock.StartIndex, LayoutCatalog.STM.Message0103.Stock.Length).Trim();

            if (int.TryParse(OriginalMessage.Body.Substring(LayoutCatalog.STM.Message0103.Operator.StartIndex, LayoutCatalog.STM.Message0103.Operator.Length), out port))
                _message.Operator = port;

            AdjustClient(OriginalMessage.Body.Substring(LayoutCatalog.STM.Message0103.Client.StartIndex, LayoutCatalog.STM.Message0103.Client.Length), out clientId, out clientDigit);
            _message.Client = clientId;

            switch (OriginalMessage.Body.Substring(LayoutCatalog.STM.Message0103.NaturezaOperacao.StartIndex, LayoutCatalog.STM.Message0103.NaturezaOperacao.Length))
            {
                case "A":
                    _message.NaturezaOperacao = Sinacor.OMM.MessageEntity.Enum.NaturezaOperacao.Compra;
                    break;
                case "V":
                    _message.NaturezaOperacao = Sinacor.OMM.MessageEntity.Enum.NaturezaOperacao.Venda;
                    break;
            }

            _message.Original = OriginalMessage.Body;

            baseMessage = TranslateMessageLabel(OriginalMessage.Label);
            _message.SequentialID = baseMessage.SequentialID;
            _message.ID = baseMessage.ID;
            _message.OperationDate = GetOperationDate(_message.ID);

            return _message;
        }

        private Sinacor.OMM.MessageEntity.BaseMessage TranslateToMessageProcess()
        {
            Sinacor.OMM.MessageEntity.STM.Message0103 _message = new Sinacor.OMM.MessageEntity.STM.Message0103();
            Sinacor.OMM.MessageEntity.BaseMessage baseMessage;
            int clientId;
            byte clientDigit;
            int port = 0;

            _message.MegaBolsaIdentificator = Sinacor.OMM.MessageEntity.Enum.MessagesMegaBolsaSource.CriacaoNegocio;
            _message.Source = Sinacor.OMM.MessageEntity.Enum.MessageSource.MegaBolsa;
            _message.Stock = OriginalMessage.Body.Substring(LayoutCatalog.STM.Message0103.Stock.StartIndex, LayoutCatalog.STM.Message0103.Stock.Length).Trim();

            if (int.TryParse(OriginalMessage.Body.Substring(LayoutCatalog.STM.Message0103.Operator.StartIndex, LayoutCatalog.STM.Message0103.Operator.Length), out port))
                _message.Operator = port;

            AdjustClient(OriginalMessage.Body.Substring(LayoutCatalog.STM.Message0103.Client.StartIndex, LayoutCatalog.STM.Message0103.Client.Length), out clientId, out clientDigit);
            _message.Client = clientId;

            switch (OriginalMessage.Body.Substring(LayoutCatalog.STM.Message0103.NaturezaOperacao.StartIndex, LayoutCatalog.STM.Message0103.NaturezaOperacao.Length))
            {
                case "A"://Message with "A" represent a buy Order Side
                    _message.NaturezaOperacao = Sinacor.OMM.MessageEntity.Enum.NaturezaOperacao.Compra;
                    _message.OrderSide = Sinacor.OMM.MessageEntity.Enum.NaturezaOperacao.Compra;
                    break;                
                case "V":
                    _message.NaturezaOperacao = Sinacor.OMM.MessageEntity.Enum.NaturezaOperacao.Venda;
                    _message.OrderSide = Sinacor.OMM.MessageEntity.Enum.NaturezaOperacao.Venda;
                    break;
            }

            _message.SystemDate = DateTime.ParseExact(OriginalMessage.Body.Substring(LayoutCatalog.STM.Message0103.SystemDate.StartIndex, LayoutCatalog.STM.Message0103.SystemDate.Length).Trim(), "yyyyMMdd", CultureInfo.InvariantCulture);
            _message.OrderQuantity = OriginalMessage.Body.Substring(LayoutCatalog.STM.Message0103.OrderQuantity.StartIndex, LayoutCatalog.STM.Message0103.OrderQuantity.Length).Trim();
            _message.Price = (Decimal.Parse((OriginalMessage.Body.Substring(LayoutCatalog.STM.Message0103.Price.StartIndex, LayoutCatalog.STM.Message0103.Price.Length).Trim())) / Decimal.Parse((Math.Pow(10, Double.Parse(OriginalMessage.Body.Substring(LayoutCatalog.STM.Message0103.DecimalPrice.StartIndex, LayoutCatalog.STM.Message0103.DecimalPrice.Length).Trim()))).ToString()));
            _message.Counterparty = OriginalMessage.Body.Substring(LayoutCatalog.STM.Message0103.Counterparty.StartIndex, LayoutCatalog.STM.Message0103.Counterparty.Length).Trim();
            _message.MegaOrder = 0;//Int32.Parse((OriginalMessage.Body.Substring(LayoutCatalog.STM.Message0103.MegaOrder.StartIndex, LayoutCatalog.STM.Message0103.MegaOrder.Length).Trim()));
            _message.Security = (OriginalMessage.Body.Substring(LayoutCatalog.STM.Message0103.Security.StartIndex, LayoutCatalog.STM.Message0103.Security.Length).Trim());
            _message.Original = OriginalMessage.Body;

            baseMessage = TranslateMessageLabel(OriginalMessage.Label);
            _message.SequentialID = baseMessage.SequentialID;
            _message.ID = baseMessage.ID;
            _message.FunctionCode = Int32.Parse((OriginalMessage.Body.Substring(LayoutCatalog.STM.Message0103.FunctionCode.StartIndex, LayoutCatalog.STM.Message0103.FunctionCode.Length).Trim()));

            return _message;
        }
    }

}